﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core;
using System.Data.Objects.DataClasses;

namespace DoodJetView.VuesPartielles
{
    /// <summary>
    /// Logique d'interaction pour EmployerForm_BrevetPermis.xaml
    /// </summary>
    public partial class EmployerForm_BrevetPermis : UserControl
    {
        private MainWindow mainWindow;
        public EmployerForm_contrat precedent;
        private EmployerForm_contrat employerForm_contrat;
        private Employes.EtapesEmployes etapesEmployes;
    
        public EmployerForm_BrevetPermis(MainWindow mainWindow)
        {
            InitializeComponent();
            comboBox_LoadForm();
            this.mainWindow = mainWindow;
        }
         
        public EmployerForm_BrevetPermis(MainWindow mainWindow, EmployerForm_contrat precedent)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            comboBox_LoadForm();
            this.mainWindow = mainWindow;
            this.precedent = precedent;
            foreach (ComboBoxItem item in comboBoxTypePermis.Items)
            {
                if (item.Content.ToString() == "Côtier")
                {
                    comboBoxTypePermis.SelectedItem = item;
                }
            }
        }

        public EmployerForm_BrevetPermis()
        { }

        public EmployerForm_BrevetPermis(Employes.EtapesEmployes etapesEmployes)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            comboBox_LoadForm();
            foreach (ComboBoxItem item in comboBoxTypePermis.Items)
            {
                if (item.Content.ToString() == "Côtier")
                {
                    comboBoxTypePermis.SelectedItem = item;
                }
            }
            this.etapesEmployes = etapesEmployes;
        }

        private void checkBoxPermis_Checked(object sender, RoutedEventArgs e)
        {
            labelTypePermis.Visibility = Visibility.Visible;
            comboBoxTypePermis.Visibility = Visibility.Visible;
            labelPermis.Visibility = Visibility.Visible;
            textBoxNumeroPermis.Visibility = Visibility.Visible;
        }
        private void checkBoxPermis_Unhecked(object sender, RoutedEventArgs e)
        {
            labelTypePermis.Visibility = Visibility.Hidden;
            comboBoxTypePermis.Visibility = Visibility.Hidden;
            labelPermis.Visibility = Visibility.Hidden;
            textBoxNumeroPermis.Visibility = Visibility.Hidden;
        }


        private void comboBox_LoadForm()
        {
            using (DataEntities cont = new DataEntities())
            {
                List<brevet> listeBrevet = null;

                listeBrevet = cont.brevet.ToList();
                foreach (brevet typeB in listeBrevet)
                {
                    comboBoxTypeBrevet.Items.Add(
                    new brevet_poco
                    {
                        id = (int)typeB.id_brevet, 
                        libelleBrevet = typeB.libelleBrevet });
                }
                var i = 0;
                foreach (brevet_poco typeB in comboBoxTypeBrevet.Items)
                {
                    if (typeB.libelleBrevet == "BEES")
                    {
                        comboBoxTypeBrevet.SelectedIndex = i;
                        return;
                    }
                    i++;
                }
            }

        }

        private void btn_Submit_Click(object sender, RoutedEventArgs e)
        {
            //TODO : enregistrement de l'employé dans la base de données
            using (DataEntities db = new DataEntities())
            {
               db.employes.AddObject(new employes
                {
                    nomEmploye = this.etapesEmployes.Etape1.txtb_nom.Text,
                    prenomEmploye = this.etapesEmployes.Etape1.txtb_prenom.Text,
                    //TODO : implementer téléphone employé
                    //telephone = this.etapesEmployes.Etape1.txtb_telephone.Text,
                    //TODO : implementer adresse employé
                    //adresseEmployes = this.etapesEmployes.Etape1.txtb_adresse.Text,
                    //TODO : implementer codePostal employé
                    //codePostal = this.etapesEmployes.Etape1.txtb_codePostal.Text,
                    //TODO : implementer ville employé
                    //codePostal = this.etapesEmployes.Etape1.txtb_ville.Text,
                    numSecu = this.etapesEmployes.Etape1.txtb_numSecu.Text,
                    //DateVisiteMed = this.etapesEmployes.Etape1.datePicker_DateVisiteMedical.SelectedDate,
                });
                if(etapesEmployes.Etape3.checkBoxPermis.IsChecked == true)
                {
                    var typePermis = etapesEmployes.Etape3.comboBoxTypePermis.SelectedItem.ToString();
                     EntityCollection<employes> emp =new EntityCollection<employes>();
                           emp.Add(this.etapesEmployes.Etape1.employer);
                    switch (typePermis)
	                {
                      
                        case "Côtier" :
                            db.permis.AddObject(new permis
                            {
                                
                                numeroDePermis = this.etapesEmployes.Etape3.textBoxNumeroPermis.Text,
                                dateObtentionPermis = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)this.etapesEmployes.Etape3.datePickerDateObtentionPermis.SelectedDate),
                                isCotier = 1,
                                isEauxInterieur = 0,
                                isPlaisance = 0,
                                isHauturier = 0,
                                employes = emp
                            });
                            break;
                        case "Hauturier":
                            db.permis.AddObject(new permis
                            {
                                numeroDePermis = this.etapesEmployes.Etape3.textBoxNumeroPermis.Text,
                                dateObtentionPermis = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)this.etapesEmployes.Etape3.datePickerDateObtentionPermis.SelectedDate),
                                isCotier = 0,
                                isEauxInterieur = 0,
                                isPlaisance = 0,
                                isHauturier = 1,
                                employes = emp
                            });
                            break;
                        case "Eaux Intérieures":
                            db.permis.AddObject(new permis
                            {
                                numeroDePermis = this.etapesEmployes.Etape3.textBoxNumeroPermis.Text,
                                dateObtentionPermis = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)this.etapesEmployes.Etape3.datePickerDateObtentionPermis.SelectedDate),
                                isCotier = 0,
                                isEauxInterieur = 1,
                                isPlaisance = 0,
                                isHauturier = 0,
                                employes = emp
                            });
                            break;
                        case "Grande Plaisance Eaux Intérieures":
                            db.permis.AddObject(new permis
                            {
                                numeroDePermis = this.etapesEmployes.Etape3.textBoxNumeroPermis.Text,
                                dateObtentionPermis = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)this.etapesEmployes.Etape3.datePickerDateObtentionPermis.SelectedDate),
                                isCotier = 0,
                                isEauxInterieur = 0,
                                isPlaisance = 1,
                                isHauturier = 0,
                                employes = emp
                            });
                            break;
		                default:
                    break;
	                }

                  
                }
            }
        }

        private void checkBoxBrevet_Checked(object sender, RoutedEventArgs e)
        {
            labelTypeBrevet.Visibility = Visibility.Visible;
            comboBoxTypeBrevet.Visibility = Visibility.Visible;
        }
        private void checkBoxBrevet_Unhecked(object sender, RoutedEventArgs e)
        {
            labelTypeBrevet.Visibility = Visibility.Hidden;
            comboBoxTypeBrevet.Visibility = Visibility.Hidden;
        }

        private void buttonPrecedent_qualification_contrat_Click(object sender, RoutedEventArgs e)
        {
            this.etapesEmployes.ContentUsers.Children.Clear();
            this.etapesEmployes.ContentUsers.Children.Add(
                    this.etapesEmployes.Etape2);
        }
    }
}
